package io.seata.samples.integration.storage.dubbo;


//import io.seata.core.context.RootContext;
import io.seata.samples.integration.common.dto.CommodityDTO;
import io.seata.samples.integration.common.dubbo.StorageDubboService;
import io.seata.samples.integration.common.response.ObjectResponse;
import io.seata.samples.integration.common.storage.GreeterGrpc;
import io.seata.samples.integration.storage.entity.TStorage;
import io.seata.samples.integration.storage.service.ITStorageService;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;
import io.seata.samples.integration.common.storage.*;

/**
 * @Author: heshouyou
 * @Description
 * @Date Created in 2019/1/23 16:13
 */

@GrpcService
@Slf4j
public class StorageDubboServiceImpl extends GreeterGrpc.GreeterImplBase {

    @Autowired
    private ITStorageService storageService;

    @Override
    public void decreaseStorage(StorageRequest request,
                io.grpc.stub.StreamObserver<StorageResponse> responseObserver)  {
        ObjectResponse objectResponse
                = storageService.decreaseStorage(request.getCommodityCode(), request.getCount());

        StorageResponse build = StorageResponse.newBuilder()
                .setStatus(objectResponse.getStatus())
                .setMessage(objectResponse.getMessage())
                .build();
        responseObserver.onNext(build);
        responseObserver.onCompleted();
    }

    @Override
    public void getStorage(com.google.protobuf.Empty request,
                           io.grpc.stub.StreamObserver<StorageLeftResponse> responseObserver){
        Integer count = storageService.selectById(1).getCount();
        StorageLeftResponse storageLeftResponse = StorageLeftResponse.newBuilder()
                .setLeft(count).build();

        responseObserver.onNext(storageLeftResponse);
        responseObserver.onCompleted();
    }
}
